package com.dudu.controller;

import com.dudu.model.OrgInfo;
import com.dudu.model.vo.CommonRequestVO;
import com.dudu.service.OrgService;
import com.dudu.util.ControllerUtils;
import com.dudu.util.ResponseBuilder;
import com.dudu.util.WebResponse;
import com.dudu.util.WebResponseHelper;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * Created by dudu on 2016/12/24.
 */
@Controller
@RequestMapping("/v1/org/")
public class OrgController {
    @Resource
    private OrgService orgService;

    @RequestMapping("/orgInfo.json")
    @ResponseBody
    public WebResponse<?> getOrgInfo(@RequestBody CommonRequestVO params) {
        return ControllerUtils.doHandler(() -> {
            OrgInfo orgInfo = orgService.getById(params.getOrgId());
            return ResponseBuilder.create().setData(orgInfo).build();
        }, "user.v1.orgInfo", "");
    }


    @RequestMapping("/create.json")
    @ResponseBody
    public WebResponse<?> createOrg(@RequestBody CommonRequestVO params) {
        return ControllerUtils.doHandler(() -> {
            orgService.create(params.getName());
            return WebResponseHelper.success();
        }, "user.v1.org.create", "");
    }

    /**
     * 申请加入一个机构*
     * @param params
     * @return
     */
    @RequestMapping("/join.json")
    @ResponseBody
    public WebResponse<?> joinOrg(@RequestBody CommonRequestVO params) {
        return ControllerUtils.doHandler(() -> {
            orgService.joinOrg(params.getOrgId());
            return WebResponseHelper.success();
        }, "user.v1.org.join", "");
    }

    @RequestMapping("/pass.json")
    @ResponseBody
    public WebResponse<?> pass(@RequestBody CommonRequestVO params) {
        return ControllerUtils.doHandler(() -> {
            orgService.joinOrg(params.getOrgId());
            return WebResponseHelper.success();
        }, "user.v1.org.join", "");
    }

    @RequestMapping("/refuse.json")
    @ResponseBody
    public WebResponse<?> refuse(@RequestBody CommonRequestVO params) {
        return ControllerUtils.doHandler(() -> {
            orgService.joinOrg(params.getOrgId());
            return WebResponseHelper.success();
        }, "user.v1.org.join", "");
    }
}
